package trans.service;

import java.util.Date;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import trans.action.support.PageMode;
import trans.model.Gatway;
import trans.model.User;
import trans.model.UserTips;
import trans.support.Constants;
import trans.utils.HibernateUtil;

public class UserDAO {
	
	public static User getUser(String userName,String password) throws Exception{
		return (User)  HibernateUtil.getSession().createCriteria(User.class)
		    .add( Restrictions.eq(User.PROP_USERNAME, userName) )
		    .add( Restrictions.eq(User.PROP_PASSWORD, password) )
		    .add( Restrictions.eq(User.PROP_STATUS, Constants.STATUS_ENABLE))
		    .uniqueResult();
	}
	
	@SuppressWarnings("unchecked")
	public static List<User> getCustomers() throws Exception{
		return  HibernateUtil.getSession().createCriteria(User.class)
		    .add( Restrictions.eq(User.PROP_USER_ROLE, Constants.ROLE_CUSTOMER) )
		    .add( Restrictions.eq(User.PROP_STATUS, Constants.STATUS_ENABLE))
		    .list();
	}
	
	@SuppressWarnings("unchecked")
	public static List<User> getSysusers() throws Exception{
		return  HibernateUtil.getSession().createCriteria(User.class)
		    .add( Restrictions.or(Restrictions.eq(User.PROP_USER_ROLE, Constants.ROLE_SYSUSER), Restrictions.eq(User.PROP_USER_ROLE, Constants.ROLE_ADMIN)) )
		    .add( Restrictions.eq(User.PROP_STATUS, Constants.STATUS_ENABLE))
		    .list();
	}
	
	@SuppressWarnings("unchecked")
	public static void fillUserPage(PageMode pm) throws Exception{
		Criteria criteria =
			 HibernateUtil.getSession().createCriteria(User.class);
		criteria.add(Restrictions.eq(Gatway.PROP_STATUS, Constants.STATUS_ENABLE));
		criteria.addOrder(Order.desc(User.PROP_USER_ROLE));
		pm.setDatas(criteria.list());
	}

	@SuppressWarnings("unchecked")
	public static boolean saveOrUpdateUser(User user) throws Exception{
		if(user.getId() == null){
			List<User> exsitUser =  HibernateUtil.getSession().createCriteria(User.class)
			.add( Restrictions.or(Restrictions.eq(User.PROP_USERNAME, user.getUsername()), 
					Restrictions.eq(User.PROP_USER_CODE, user.getUserCode())) )
					.list();
			if(exsitUser.isEmpty() == false && user.getIsCustomer() == false){
				return false;
			}
			user.setPassword(user.getUsername());
			user.setStatus(Constants.STATUS_ENABLE);
		}
		
		if(!user.getIsCustomer()){
			user.setUserCode("---");
			user.setUserCompName("---");
		}
		
		user.setCreateTime(new Date());
		HibernateUtil.saveOrUpdate(user);
		return true;
	}
	
	public static void updateUserStatus(Integer userId,Integer userStatus) throws Exception{
		User user = getUser(userId);
		if(user.getUserRole() == Constants.ROLE_ADMIN){
			return;
		}
		user.setStatus(userStatus);
		user.setCreateTime(new Date());
		HibernateUtil.saveOrUpdate(user);
	}
	
	public static User getUser(Integer userId) throws Exception{
		User user =
			(User) HibernateUtil.get(User.class, userId);
		return user;
	}
	
	public static User getCustomerByCode(String clientCode){
		User user = (User)  HibernateUtil.getSession().createCriteria(User.class)
	    .add( Restrictions.eq(User.PROP_USER_CODE, clientCode) )
	    .add( Restrictions.eq(User.PROP_STATUS, Constants.STATUS_ENABLE))
	    .uniqueResult();
		return user;
	}
	
	public static void deleteTip(Integer tipId){
		UserTips userTip =
			(UserTips) HibernateUtil.get(UserTips.class, tipId);
		HibernateUtil.delete(userTip);
	}
	
	@SuppressWarnings("unchecked")
	public static void saveTip(Integer clientId,Integer type,String value){
		List<UserTips> exsitTips =  HibernateUtil.getSession().createCriteria(UserTips.class)
		 .add( Restrictions.eq(UserTips.PROP_USER_ID, clientId))
		 .add(Restrictions.eq(UserTips.PROP_TIP_TYPE, type))
		 .add(Restrictions.eq(UserTips.PROP_TIP_VALUE, value))
		 .list();
		if(exsitTips.isEmpty() && value.isEmpty() == false && type != null && clientId != null){
			UserTips tip = new UserTips();
			tip.setTipType(type);
			tip.setTipValue(value);
			tip.setUserId(clientId);
			HibernateUtil.saveOrUpdate(tip);
		}
	}
	
	
	@SuppressWarnings("unchecked")
	public static List<UserTips> getUserTips(Integer userId){
		return (List<UserTips>)  HibernateUtil.getSession().createCriteria(UserTips.class)
	    .add( Restrictions.eq(UserTips.PROP_USER_ID, userId) ).list();
	}
}
